package com.note.simpleshop.module.order.service;
import com.note.simpleshop.entity.TbOrderItem;
import com.note.simpleshop.module.order.dao.OrderItemRepository;
import com.note.simpleshop.module.order.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderItemService {
    private static final String CACHE_NAME="order_cache";
    @Autowired
    private OrderItemRepository orderItemRepository;

    public void save(TbOrderItem orderItem) {
        orderItemRepository.save(orderItem);
    }
    @Cacheable(CACHE_NAME)
    public List<OrderItemVo> findByOrderId(String orderId) {
        List<OrderItemVo> orderItemVoList=new ArrayList<OrderItemVo>();
        List<TbOrderItem> orderItemList=orderItemRepository.findByOrderId(orderId);
        for (TbOrderItem tbOrderItem:orderItemList) {
            OrderItemVo orderItemVo=OrderItemVo.newInstance(tbOrderItem);
            orderItemVoList.add(orderItemVo);
        }
        return  orderItemVoList;
    }
}
